#!/bin/bash
set -e

programs/build_helpers/buildstep -s 3500
ccache -s
programs/build_helpers/buildstep Prepare 1 "sed -i '/tests/d' libraries/fc/CMakeLists.txt"
programs/build_helpers/buildstep cmake 5 "cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_FLAGS=--coverage -DCMAKE_CXX_FLAGS=--coverage -DBoost_USE_STATIC_LIBS=OFF -DCMAKE_CXX_OUTPUT_EXTENSION_REPLACE=ON ."
programs/build_helpers/buildstep make.programs 1800 "programs/build_helpers/make_with_sonar bw-output -j 2 witness_node cli_wallet js_operation_serializer get_dev_key network_mapper app_test chain_test cli_test"
set -o pipefail
programs/build_helpers/buildstep prepare.sonar 20 "find libraries/[acdenptuw]*/CMakeFiles/*.dir programs/[cdgjsw]*/CMakeFiles/*.dir -type d -print | while read d; do gcov -o \"\$d\" \"\${d/CMakeFiles*.dir//}\"/*.cpp; done >/dev/null; programs/build_helpers/set_sonar_branch sonar-project.properties"
du -hs sonar_cache
# The first pass, skip some files. This will remove the skipped files from the cache, but is an acceptable trade-off
programs/build_helpers/buildstep prepare.sonar.part1 1 "cp sonar-project.properties sonar-project.properties.bak; sed -i '/sonar\.exclusions=/d;s/#sonar\.exclusions.part1/sonar.exclusions/' sonar-project.properties"
programs/build_helpers/buildstep run.sonar.part1 1500 "which sonar-scanner && sonar-scanner"
programs/build_helpers/buildstep post.sonar.part1 1 "cp sonar-project.properties.bak sonar-project.properties"
du -hs sonar_cache
# The second pass, scan more files
programs/build_helpers/buildstep prepare.sonar.part2 1 "cp sonar-project.properties sonar-project.properties.bak; sed -i '/sonar\.exclusions=/d;s/#sonar\.exclusions.part2/sonar.exclusions/' sonar-project.properties"
programs/build_helpers/buildstep run.sonar.part2 1200 "which sonar-scanner && sonar-scanner"
programs/build_helpers/buildstep post.sonar.part2 1 "cp sonar-project.properties.bak sonar-project.properties"
du -hs sonar_cache
programs/build_helpers/buildstep end 0
ccache -s

